﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Axelerate.Silverlight3D.Media.Media3D;

namespace Axelerate3DEngineUseSamples
{
    public partial class MouseRotation : UserControl
    {
        bool IsRotating = false;
        Point MousePosition;
        AxisAngleRotation3D rotationX;
        AxisAngleRotation3D rotationY;
        public MouseRotation()
        {
            InitializeComponent();
            rotationX = (((this.Viewport.Children[1] as ModelVisual3D).Transform as Transform3DGroup).Children[0] as RotateTransform3D).Rotation as AxisAngleRotation3D;
            rotationY = (((this.Viewport.Children[1] as ModelVisual3D).Transform as Transform3DGroup).Children[1] as RotateTransform3D).Rotation as AxisAngleRotation3D;
            GeometryModel3D Horse = (((this.Viewport.Children[1] as ModelVisual3D).Content as Model3DGroup).Children[0] as GeometryModel3D);
            GeometryModel3D BoundingBox = (((this.Viewport.Children[1] as ModelVisual3D).Content as Model3DGroup).Children[1] as GeometryModel3D);
            BoundingBox.Geometry = PrimitiveMeshBuilder.CreateBox(Horse.Bounds);

        }

        private void Viewport_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsRotating = true;
            MousePosition = e.GetPosition(sender as UIElement);
        }

        private void Viewport_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsRotating = false;
        }

        private void Viewport_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsRotating)
            {
                // in X: the viewport.actualwidth = 360 degres :: deltaX = newAngle
                Point newPosition = e.GetPosition(sender as UIElement);
                rotationY.Angle += ((newPosition.X - MousePosition.X) * 180) / (Viewport.ActualWidth);
                rotationX.Angle += ((newPosition.Y - MousePosition.Y) * 180) / (Viewport.ActualHeight);
                
            }
        }
    }
}
